<?php
class Integral_logAction extends CommonAction {
	
	function index(){
		$member_id = $this->_get('id');
		//exit(dump($member_id));
		$this->listmap = ' member_id = '.$member_id;
		//exit(dump('member_id = '.$member_id));
		//$this->listorder="dateline desc";
		$this->member_id = $member_id;
        $this->_list();
		$this->display();
	}
	
	public function add(){

		$this->member_id = $this->_get('member_id');
		
		parent::add();
	}
	
	public function points_add(){

		$this->member_id = $this->_get('member_id');
		
		$this->display();
	}
	public function insert(){
		
		//$on = $this->_post('online');
		$off = $this->_post('offline');
		$map['id'] = $this->_post('member_id');
	
		$chanageType = $this->_post('type');
		$memberDb = D('Member');
		$member=$memberDb->where($map)->find();
		if($chanageType == 'setInc'){
			$memberDb -> where($map) -> setInc('offline', $off);
			//$memberDb -> where($map) -> setInc('online', $on);
		}else{
			
			
			$memberDb -> where($map) -> setDec('offline', $off);
			//$memberDb -> where($map) -> setDec('online', $on);
			//$off = $_POST['online'] * -1;
			$off = $_POST['offline'] * -1;  
			
		}

		
		$integral_log['remark']=$_POST['remark'];
		$integral_log['member_id']=$_POST['member_id'];
		$integral_log['total']=($off+$on)+$member['offline'];
		$integral_log['offline']=$off;
		//$integral_log['online']=$on;
		$integral_log['time']=time();
		$integral_log['order_id']=0;
		$integral_log['sale_id']=0;
		
	  
     	
		$ret=$this->dao->add($integral_log);
	
		if($ret){
			//微信通知
			
			$wxuser = $memberDb -> where($map) -> find();
			
			/*if($_POST['money'] > 0){
				$text = "尊敬的".$wxuser['nickname'].":\r\n您的账户积分余额增加".abs($integral_log['total'])."\r\n积分：".$on."\r\n通用积分：".$off."\r\n原因：".$integral_log['remark']."\r\n目前账号积分余额：".abs($wxuser['online']+$wxuser['offline'])."\r\n线上积分：".$wxuser['online']."\r\n通用积分：".$wxuser['offline'];
			}else{
					$text = "尊敬的".$wxuser['nickname'].":\r\n您的账户积分余额增加".abs($integral_log['total'])."\r\n线上积分：".$on."\r\n通用积分：".$off."\r\n原因：".$integral_log['remark']."\r\n目前账号积分余额：".abs($wxuser['online']+$wxuser['offline']);
			}*/
			sendWXMsg($wxuser['openid'], $text);
			
			$this->success('操作成功！', U('index', array('id' => $wxuser['id'])));
		}else{
			$this->error($this->dao -> getError());
		}
	}
	
	public function points_insert(){
		$total = $this->_post('total');
		$map['member_id'] = $this->_post('member_id');
		$memberDb=D('Member');
		$chanageType = $this->_post('type');
		$PointsDB = M('Points');
		if($PointsDB->where(' member_id = '.$map['member_id'])->find()){
		if($chanageType == 'setInc'){
			$PointsDB -> where($map) -> setInc('total', $total);
		}else{
			$_POST['total'] = $_POST['total'] * -1;
			$PointsDB -> where($map) -> setDec('total', $total);
		}
		}else{
		$p_data['member_id']=$map['member_id'];
		$p_data['total']=$total;
		$PointsDB ->add($p_data);
		}
		
		$data['member_id'] = $map['member_id'];
		$data['remark'] = $_POST['remark'];
		$data['time'] = time();
		$data['total'] = $_POST['total'];
		$data['form'] = 0;
		$data['member_name'] = 0;
		$data['order_id'] = 0;
		$data['product_id'] = 0;
		$data['product_name'] = 0;
		$data['model'] = 0;
		$data['product_money'] = 0;
		$data['order_sn'] = 0;
		
		
		//$cret = $this -> dao -> create($data);
		
		/*if(!$cret){
			$this->error($this->dao -> getError());
		}*/
		
		$ret = M('Points_log')-> data($data) -> add();
		if($ret){
			//微信通知
			
			$wxuser = $memberDb -> where(array('id' => $map['member_id'])) -> find();
			if($_POST['money'] > 0){
				//$text = "尊敬的".$wxuser['nickname'].":\r\n您的积分池增加".abs($_POST['money'])."\r\n原因：".$data['total']."\r\n目前余额为：".$wxuser['money'];
			}else{
				//$text = "尊敬的".$wxuser['nickname'].":\r\n您的积分池减少".abs($_POST['money'])."\r\n原因：".$data['total']."\r\n目前余额为：".$wxuser['money'];
			}
			//sendWXMsg($wxuser['openid'], $text);
			
			$this->success('操作成功！', U('Points/points_list', array('member_id' => $map['member_id'])));
		}else{
			$this->error($this->dao -> getError());
		}
	}
	
	
	
	
	
}
